export default {
  namespaced:true,
  state:()=>({
    cart:[
      {
                      "goods_count": 1,
                       "goods_id": 43986,
                      "goods_name": "海信(Hisense)LED55MU9600X3DUC 55英寸 4K超高清量子点电视 ULED画质 VIDAA系统",
                      "goods_price": 13999,
                      "goods_small_logo": "http://image5.suning.cn/uimg/b2c/newcatentries/0000000000-000000000160455569_1_400x400.jpg",
                      "goods_state": true
                      
                  },
                  {
                      "goods_id": 43985,
                      "goods_name": "海信(Hisense)LED50MU8600UC 50英寸 4K超高清智能电视 HDR超薄曲面",
                      "goods_price": 6809,
                      "goods_count": 1,
                      "goods_small_logo": "http://image5.suning.cn/uimg/b2c/newcatentries/0000000000-000000000193373779_1_400x400.jpg",
                      "goods_state": true
                  },
                  {
                      "goods_id": 43984,
                      "goods_name": "TCL电视 49P3",
                      "goods_price": 4699,
                      "goods_count": 1,
                      "goods_small_logo": "http://image4.suning.cn/uimg/b2c/newcatentries/0000000000-000000000606013705_1_400x400.jpg",
                      "goods_state": true
                  },
    ],
  }),
  mutations:{
    //加入购物车
    addToCart(state,goods){
      // console.log('加入成功！')
      const findResult=state.cart.find((x)=>x.goods_id===goods.goods_id)
      if(!findResult){
        state.cart.push(goods)
        console.log(goods)
      }else{
        findResult.goods_count++
      }
    },
    //将购物车数据保存在本地
    saveToStorage(state){
      uni.setStorageSync('cart',JSON.stringify(state.cart))
    },
    //改变勾选状态
    updateGoodsState(state,goods){
      const findResult=state.cart.find(x=>x.goods_id===goods.goods_id)
      if(findResult){
        findResult.goods_state=goods.goods_state
        this.commit('m_cart/saveToStorage')
      }
    },
    updateGoodsCount(state,goods){
      const findResult=state.cart.find(x=>x.goods_id===goods.goods_id)
      if(findResult){
        findResult.goods_count=goods.goods_count
        this.commit('m_cart/saveToStorage')
      }
    },
    removeGoodsById(state,goods_id){
      state.cart=state.cart.filter(x=>x.goods_id!==goods_id)
      this.commit('m_cart/saveToStorage')
    },
    updateAllGoodsState(state,mewState){
      // console.log("333")
      state.cart.forEach(x=>x.goods_state=mewState)
      this.commit('m_cart/saveToStorage')
    },
  emptyCart(state,cart){
      state.cart=cart
  }
  },
  getters:{
    total(state){
      let c=0
      state.cart.forEach(goods=>c+=goods.goods_count)
      return c
    },
    checkedCount(state){
      return state.cart.filter(x=>x.goods_state).reduce((total,item)=>total+=item.goods_count,0)
    },
    checkedGoodsAmount(state){
      return state.cart.filter(x=>x.goods_state).reduce((total,item)=>total+=item.goods_count*item.goods_price,0)
    }
  }
}